<?

class bf_array implements Iterator {

	protected $data;
	protected $opts;
	private $current;

	public function __construct() {
		$this->data = array();
		for ($argc = func_num_args(), $i = 0; $i < $argc; $i ++)
			$this->data[] = func_get_arg($i);
	}
    
	public function has_key($key) {
		return isset($this->data[$key]);
	}
	
	public function get() {
		$res = array();
		if (($argc = func_num_args()) == 1 && is_array($arg = func_get_arg(0))) {
			// Call type: $array->get(array('key-1' => 'default-value-1', 'key-2' => 'default-value-2', 'key-3' => 'default-value-3'))
			foreach ($arg as $key => $value)
				$res[] = (isset($this->data[$key]) ? $this->data[$key] : $value);
		} else {
			// Call type: $array->get('key-1', 'key-2', 'key-3')
			$res = array();
			for ($i = 0; $i < $argc; $i ++)
				$res[] = @$this->data[func_get_arg($i)];
		}
		return (count($res) == 1 ? $res[0] : $res);
	}
	
	public function set() {
		if (($argc = func_num_args()) == 1 && is_array($arg = func_get_arg(0))) {
			// Call type: $array->set(array('key-1' => 'value-1', 'key-2' => 'value-2', 'key-3' => 'value-3'))
			foreach ($arg as $key => $value)
				$this->data[$key] = $value;
		} else if ($argc == 2) {
			// Call type: $array->set('key', 'value')
			$this->data[func_get_arg(0)] = func_get_arg(1);
		} else if ($argc == 1) {
			// Call type: $array->set('key')
			$this->data[func_get_arg(0)] = null;
		}
		return $this;
	}

	public function remove() {
		if (($argc = func_num_args()) == 1 && is_array($arg = func_get_arg(0))) {
			// Call type: $array->remove(array('key-1', 'key-2', 'key-3'))
			foreach ($arg as $key)
				unset($this->data[$key]);
		} else {
			// Call type: $array->remove('key-1', 'key-2', 'key-3')
			for ($argc = func_num_args(), $i = 0; $i < $argc; $i ++)
				unset($this->data[func_get_arg($i)]);
		}
		return $this;
	}

	public function each($callback, $param = null) {
		foreach ($this->data as $key => $value)
			call_user_func($callback, array($value, $key, $this, $param));
		return $this;
	}
	
	public function length() {
		return count($this->data);
	}

	public function wrap($s = "'", $e = false) {
		if ($e === false) $e = $s;
		return $this->each(create_function('$p', 'list ($v,$k,$a,$w) = $p; $a->set($k, $w[0].$v.$w[1]);'), array($s, $e));
	}

	public function join($d = '') {
		return implode($d, $this->data);
	}
	
	public function to_array() {
		return $this->data;
	}
	
	static public function from_array($array) {
		$res = new bf_array();
		$res->data = $array;
		return $res;
	}

	static public function to_dictionary($array, $key_column = 'id', $value_column = 'name') {
		$res = new bf_array();
		foreach ($array as $row) {
			if (is_object($row)) {
				$res->set($row->$key_column, $row->$value_column);
			} else {
				$res->set($row[$key_column], $row[$value_column]);
			}
		}
		return $res;
	}

	public function rewind() {
		reset($this->data);
		$this->current = each($this->data);
	}

	public function valid() {
		return isset($this->data[$this->key()]);
	}

	public function next() {
		$this->current = each($this->data);
	}

	public function key() {
		return $this->current['key'];
	}

	public function current() {
		return $this->current['value'];
	}
}

?>